package com.xyx.yfy_site_msg.system.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyx.yfy_site_msg.system.entity.YfyDemand;
import com.xyx.yfy_site_msg.system.service.YfyDemandService;
import com.xyx.yfy_site_msg.system.utils.AppFileUtils;
import com.xyx.yfy_site_msg.system.utils.DataGridView;
import com.xyx.yfy_site_msg.system.utils.ResultObj;
import com.xyx.yfy_site_msg.system.vo.YfyDemandVo;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 需求前端控制器
 * </p>
 *
 * @author xyx
 * @since 2021-01-27
 */
@Log4j2
@RestController
@RequestMapping("demand")
public class YfyDemandController {
    @Autowired
    private YfyDemandService demandService;

    /**
     * 分页查询需求记录
     *
     * @param demandVo
     * @return
     */
    @RequestMapping("loadAllDemand")
    public DataGridView loadAllDemand(YfyDemandVo demandVo) {
        Page<YfyDemand> page = new Page<>(demandVo.getPage(), demandVo.getLimit());
        QueryWrapper<YfyDemand> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(demandVo.getCustomer()), "customer", demandVo.getCustomer());
        queryWrapper.like(StringUtils.isNotBlank(demandVo.getDemandby()), "demandby", demandVo.getDemandby());
        queryWrapper.like(StringUtils.isNotBlank(demandVo.getMarket()), "market", demandVo.getMarket());
        queryWrapper.ge(null != demandVo.getFeedStartTime(), "feedbacktime", demandVo.getFeedStartTime());
        queryWrapper.le(null != demandVo.getFeedEndTime(), "feedbacktime", demandVo.getFeedEndTime());
        queryWrapper.ge(null != demandVo.getTheStartTime(), "actualtime", demandVo.getTheStartTime());
        queryWrapper.le(null != demandVo.getTheEndTime(), "actualtime", demandVo.getTheEndTime());
        queryWrapper.orderByDesc("id");
        this.demandService.page(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 添加需求记录
     *
     * @param demandVo
     * @return
     */
    @RequestMapping("addDemand")
    public ResultObj addDemand(YfyDemandVo demandVo) {
        try {
            if (demandVo.getFile() != null && demandVo.getFile().endsWith("_temp")) {
                String newName = AppFileUtils.renameFile(demandVo.getFile());
                demandVo.setFile(newName);
            }
            this.demandService.save(demandVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改需求记录
     *
     * @param demandVo
     * @return
     */
    @RequestMapping("updateDemand")
    public ResultObj updateDemand(YfyDemandVo demandVo) {
        try {
            this.demandService.updateById(demandVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据id删除一条需求记录
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteDemand")
    public ResultObj deleteDemand(Integer id) {
        try {
            this.demandService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除选中的需求记录
     *
     * @param ids
     * @return
     */
    @RequestMapping("batchDeleteDemand")
    public ResultObj batchDeleteDemand(Integer[] ids) {
        try {
            Collection<Serializable> list = new ArrayList<>();
            for (Integer id : ids) {
                list.add(id);
            }
            this.demandService.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 文件上传接口
     *
     * @param multipartFile
     * @return
     */
    @RequestMapping("uploadFile")
    public Map<String, Object> uploadFile(MultipartFile multipartFile) {
//        1、得到文件名
        String originalFilename = multipartFile.getOriginalFilename();
//        2、根据旧文件名得到新文件名
        String newFileName = AppFileUtils.createNewFileName(originalFilename);
//        3、得到当前日期的字符串
        String dirName = DateUtil.format(new Date(), "yyyy-MM-dd");
//        4、构造文件夹
        File dirFile = new File(AppFileUtils.UPLOAD_PATH, dirName);
//        5、判断文件夹是否存在，如果不存在则创建
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
//        6、构造文件对象
//        File file = new File(dirFile, newFileName + "_temp");
        File file = new File(dirFile, newFileName);
//        7、把mutipartFile里面的信息写入file
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<>();
//        map.put("path", dirName + "/" + newFileName + "_temp");
        map.put("path", dirName + "/" + newFileName);
        return map;
    }


}

